﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using System.Text.RegularExpressions;
using Job.Logic.Personal;
using Job.Entity;
using System.IO;
using Job.Logic.Common;
using Job.Logic.BaseContent;


namespace Job.Web.userControls.Admin
{
    public partial class LogoManager : BaseCtrl
    {
        private int controlType
        {
            get { return myR["controlType"].IntValue; }
        }

        private int advId
        {
            get { return myR["advId"].IntValue; }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (advId > 0)
                {
                    BaseContentLogic bl = new BaseContentLogic();
                    JobAdvanceCustomer ac = bl.GetAdvancebyId(advId);
                    txtTitle.Text = ac.Title.Trim();
                    txtHomePage.Text = ac.HomePage.Trim();
                    txtOrderNo.Text = ac.OrderNo.ToString();
                    string filePath =(System.Configuration.ConfigurationManager.AppSettings["LogoFolder"]);
                    Image1.ImageUrl = filePath + ac.FileName;
                }
                else
                {
                    Image1.ImageUrl = WebCommon.GetApplicationPath(Request) + "Images/resume.gif";
                }
            }
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            string _regex = @"^.+\.(jp?g|gif)$";
            Regex test = new Regex(_regex);
            if (advId > 0)
            {
                this.Save();
            }
            else
            {
                if (test.IsMatch(fileUpload.PostedFile.FileName.ToLower()) == true)
                {
                    this.Save();
                }
                else
                {
                    string message = Server.UrlEncode(String.Format("保存失败，请与管理员联系！按返回按钮重新输入。错误信息: {0}!", "不是有效的图片(jpg,jpeg.gif)"));
                    string urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + Convert.ToString((int)Constants.SubjectType.AdvanceCustomer));
                    string url = string.Format("~/ErrorMessage.aspx?flag=0&message={0}&returnURL={1}", message, urlPath);
                    Response.Redirect(url);
                }
            }
            
        }

        private void Save()
        {
            string filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["LogoFolder"]);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            BaseContentLogic bl = new BaseContentLogic();

            JobAdvanceCustomer ac = null;
            if (advId > 0)
                ac = bl.GetAdvancebyId(advId);
            else
                ac = new JobAdvanceCustomer();

            string fileName = Guid.NewGuid().ToString()+Path.GetExtension(fileUpload.FileName);
            if (fileUpload.FileName.Trim().Length > 1)
            {
                ac.FileName = fileName;// Guid.NewGuid().ToString() + Path.GetExtension(fileUpload.FileName); 
            }
            ac.HomePage = txtHomePage.Text.Trim();
            ac.Title = txtTitle.Text.Trim();
            int orderno = 0;
            int.TryParse(txtOrderNo.Text.Trim(),out orderno);
            ac.OrderNo = orderno;
            try
            {
                if (fileUpload.FileName.Trim().Length > 1)
                {
                    fileUpload.SaveAs(Path.Combine(filePath, ac.FileName));
                }

                if (ac.AdvanceCustomerId > 0)
                {
                    bl.UpdateAdv(ac);
                }
                else
                {
                    bl.InsertAdvanceCustomer(ac);
                }

                Image1.ImageUrl = WebCommon.GetApplicationPath(Request) + filePath + "/" + ac.FileName;
                string message = message = Server.UrlEncode("保存成功，请按返回按钮查看信息!");
                string urlPath = string.Empty;
                string url = string.Empty;

                urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + (int)Constants.SubjectType.AdvanceCustomerList);
                url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
                Response.Redirect(url, false);

            }
            catch (Exception ex)
            {
                string message = Server.UrlEncode(String.Format("保存失败，请与管理员联系！按返回按钮重新输入。错误信息: {0}!", ex.Message));
                string urlPath = Server.UrlEncode("~/AdminWeb/AdminMain.aspx?controlType=" + Convert.ToString((int)Constants.SubjectType.AdvanceCustomer));
                string url = string.Format("~/ErrorMessage.aspx?flag=0&message={0}&returnURL={1}", message, urlPath);
                Response.Redirect(url);
            }
        }
    }
}